#include "main.h"
#include "BTK05.h"
#include "usart.h"
#include "RGB.h"
#include "tim.h"
unsigned char BTK05_ATKeyDataPack[12] = 
{
	BTK05_CMD_BYTE1 ,
	BTK05_CMD_BYTE2 ,
	BTK05_CMD_BYTE3 ,
	BTK05_CMD_BYTE4 ,
	0x00 ,
	BTK05_CMD_BYTE6 , 
	0x00 ,
	0x00 ,
	0x00 ,
	0x00 ,
	0x00 ,
	0x00 
};

unsigned char ATReturnSuccessedFlag = 0;
unsigned char RxBuffer[50] ;
unsigned char RxBuffer_Tail = 0;
unsigned char RxBuffer_OK[10];
unsigned char RxBuffer_OK_Tail = 0;

unsigned char BTK05_ConsumerPack[8] = {0x08,0x00,0xA1,0x03,0x00,0x00,0x00,0x00};

enum BTK05_StatusTypedef BTK05_Status = BTK_SLEEP;


//发送字节
void BTK05_UART_SendChar(unsigned char txChar)
{
	HAL_UART_Transmit(&huart1,&txChar,sizeof(txChar),0xffff);
	while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX);
}

//发送键盘文件
void BTK05_UART_SendKeyData(unsigned char* KeyDataBuffer, unsigned char byte_lenth)
{
	unsigned char i;
	for(i = 0; i < byte_lenth; i++)
	{
		BTK05_UART_SendChar(*(KeyDataBuffer+i));
	}
}

//发送蓝牙命令
unsigned char BTK05_UART_SendATCmd(unsigned char* ATBuffer)
{
	unsigned char timeOutTemp = 0;
	HAL_UART_Transmit(&huart1,(uint8_t *)ATBuffer,sizeof(ATBuffer),0xfff);
	Delay_us(500);
	BTK05_UART_SendChar(0x0D);
	Delay_us(500);
	BTK05_UART_SendChar(0x0A);
	Delay_us(500);
	while(!ATReturnSuccessedFlag)
	{
		HAL_Delay(1);
		timeOutTemp++;
		if(timeOutTemp == 200)
		{
			Full_COLOR(RED);
			return 0;//AT设置失败
		}
	}
	
	if(RxBuffer_OK[0] == 'O' && RxBuffer_OK[1] == 'K')
	{
		ATReturnSuccessedFlag = 0;
		RxBuffer_OK_Tail = 0;
		return 1;//设置成功
		
	}
	ATReturnSuccessedFlag = 0;
	RxBuffer_OK_Tail = 0;
	
	return 0;
}

//进入AT指令模式
unsigned char BTK05_ATMode()
{
	HAL_GPIO_WritePin(GPIOC,ATMode_Pin,1);
	HAL_Delay(400);
	HAL_GPIO_WritePin(GPIOC,ATMode_Pin,0);
	return BTK05_UART_SendATCmd((unsigned char*)"AT");;
}

//退出AT指令模式
unsigned char BTK05_KEYMode()
{
	return BTK05_UART_SendATCmd((unsigned char*)"AT+EXIT");
}

//进入匹配模式
void BTK05_PairMode()
{
	if(BTK05_ATMode())
	{
		BTK05_UART_SendATCmd((unsigned char*)BTK05_AT_ENPairMode);
		HAL_Delay(500);
		BTK05_KEYMode();
	}
}

//进入省电模式
void BTK05_LowEnerageMode()
{
	if(BTK05_ATMode())
	{
		BTK05_UART_SendATCmd((unsigned char*)BTK05_AT_SLEEPMODE);
		HAL_Delay(500);
		BTK05_KEYMode();
	}
}
	
//复位
void BTK05_RESET()
{
	if(BTK05_ATMode())
	{
		BTK05_UART_SendATCmd((unsigned char*)BTK05_AT_RESET);
		HAL_Delay(500);
		BTK05_KEYMode();
	}
}

//删除已经链接的主机信息
void BTK05_DeleteConnectInfo()
{
	if(BTK05_ATMode())
	{
		BTK05_UART_SendATCmd((unsigned char*)BTK05_AT_DeletePaired);
		HAL_Delay(500);
		BTK05_KEYMode();
	}
}

//退出睡眠模式
void BTK05_Wake()
{
	HAL_GPIO_WritePin(WakeUp_GPIO_Port,WakeUp_Pin,1);
	BTK05_Status = BTK_WAKE;
	HAL_Delay(20);
}

//进入睡眠模式
void BTK05_Sleep()
{
	HAL_GPIO_WritePin(WakeUp_GPIO_Port,WakeUp_Pin,0);
	BTK05_Status = BTK_SLEEP;
	HAL_Delay(20);
}

void USB_Mode(void)
{
	g_USBModeFlag = TRUE;
	
	//使BTK05进入休眠模式
	BTK05_Sleep();
	sleepTime1SCounter = 0;
	//USB对应的灯亮起
	HAL_TIM_Base_Start_IT(&htim2);
	/*此处应有函数*/
}

void BLUETEETH_Mode(void)
{
	g_USBModeFlag = FALSE;
	
	BTK05_Wake();
	HAL_TIM_Base_Start_IT(&htim2);
	sleepTime1SCounter = 0;
}
	
	
	